#!/usr/bin/perl
use strict;
use CGI::Fast;
use LWP::UserAgent;
use URI::Escape;
use XML::LibXML;
use Encode;

my @Locations = (
		[1,'稚内',45.416995,141.676999],
		[2,'旭川',43.763492,142.357803],
		[3,'留萌',43.943001,141.651428],
		[4,'札幌',43.068612,141.350768],
		[5,'岩見沢',43.204201,141.759359],
		[6,'倶知安',42.901640,140.745471],
		[7,'網走',44.020024,144.254158],
		[8,'北見',43.805504,143.898167],
		[9,'紋別',44.356736,143.354094],
		[10,'根室',43.326923,145.582707],
		[11,'釧路',42.990584,144.381983],
		[12,'帯広',42.917997,143.201950],
		[13,'室蘭',42.317864,140.975710],
		[14,'浦河',42.168366,142.769976],
		[15,'函館',41.773709,140.726413],
		[16,'江差',41.856727,140.127933],
		[17,'青森',40.828668,140.734738],
		[18,'むつ',41.286576,141.166104],
		[19,'八戸',40.509200,141.431499],
		[20,'秋田',39.716909,140.129929],
		[21,'横手',39.310146,140.560400],
		[22,'盛岡',39.701547,141.136599],
		[23,'宮古',39.640287,141.946454],
		[24,'大船渡',39.063529,141.720807],
		[25,'仙台',38.260196,140.882159],
		[26,'白石',38.002808,140.61985],
		[27,'山形',38.248266,140.327253],
		[28,'米沢',37.909644,140.128212],
		[29,'酒田',38.922358,139.84599],
		[30,'新庄',38.762560,140.306064],
		[31,'福島',37.754123,140.459680],
		[32,'小名浜',36.948148,140.895950],
		[33,'若松',33.901179,130.806307],
		[34,'静岡',34.971629,138.388579],
		[35,'網代',35.043718,139.081249],
		[36,'三島',35.126336,138.910061],
		[37,'浜松',34.704038,137.734759],
		[38,'名古屋',35.170694,136.881637],
		[39,'豊橋',34.762734,137.382128],
		[40,'岐阜',35.409967,136.756324],
		[41,'高山',36.140574,137.251301],
		[42,'津',34.734417,136.510580],
		[43,'尾鷲',34.074479,136.190171],
		[44,'富山',36.701384,137.213091],
		[45,'伏木',36.792481,137.058531],
		[46,'金沢',36.578117,136.648166],
		[47,'輪島',37.388397,136.932129],
		[48,'福井',36.061479,136.223261],
		[49,'敦賀',35.644767,136.076488],
		[50,'新潟',37.912299,139.060869],
		[51,'長岡',37.447787,138.853927],
		[52,'高田',37.109622,138.25691],
		[53,'相川',34.757942,135.533853],
		[54,'水戸',36.370911,140.476760],
		[55,'土浦',36.078644,140.206238],
		[56,'宇都宮',36.559246,139.898389],
		[67,'大田原',36.859563,140.024388],
		[58,'前橋',36.383399,139.072833],
		[59,'水上',36.778614,138.968800],
		[60,'さいたま',35.948602,139.555813],
		[61,'熊谷',36.139627,139.389528],
		[62,'秩父',35.998830,139.086044],
		[63,'東京',35.681391,139.766103],
		[64,'大島',34.750217,139.355575],
		[65,'八丈島',33.116266,139.782819],
		[66,'父島',27.091578,142.198432],
		[67,'千葉',35.613425,140.112837],
		[68,'銚子',35.729449,140.827557],
		[69,'館山',34.995900,139.861888],
		[70,'横浜',35.465888,139.622901],
		[71,'小田原',35.256271,139.155043],
		[72,'長野',36.643307,138.189101],
		[73,'松本',36.230393,137.964450],
		[74,'飯田',35.519722,137.821244],
		[75,'甲府',35.667054,138.569015],
		[76,'河口湖',35.498231,138.768852],
		[77,'大津',35.002997,135.864651],
		[78,'彦根',35.272584,136.263453],
		[79,'京都',34.985705,135.758228],
		[80,'舞鶴',35.460380,135.355012],
		[81,'大阪',34.702398,135.495188],
		[82,'神戸',34.679629,135.178217],
		[83,'豊岡','35.544161',134.813485],
		[84,'奈良',34.680661,135.818932],
		[85,'風屋',34.042916,135.782862],
		[86,'和歌山',34.232436,135.191454],
		[87,'潮岬',33.449102,135.764503],
		[88,'岡山',34.666572,133.918552],
		[89,'津山',35.054511,134.003477],
		[90,'広島',34.397446,132.475593],
		[91,'庄原',34.894923,133.068901],
		[92,'松江',35.463947,133.063871],
		[93,'浜田',34.902862,132.088591],
		[94,'西郷',36.226579,133.309361],
		[95,'鳥取',35.493953,134.225901],
		[96,'米子',35.423270,133.336570],
		[97,'下関',33.950765,130.92316],
		[98,'山口',34.076499,131.506760],
		[99,'柳井',33.965728,132.105988],
		[100,'萩',34.393845,131.401210],
		[101,'徳島',34.074746,134.551395],
		[102,'日和佐',33.728954,134.530603],
		[103,'高松',34.350806,134.046818],
		[104,'松山',33.840007,132.75115],
		[105,'新居浜',33.948124,133.294761],
		[106,'宇和島',33.225774,132.567460],
		[107,'高知',33.56693,133.543432],
		[108,'室戸',33.315782,134.144139],
		[109,'足摺',32.72676,133.009343],
		[110,'福岡',33.606756,130.418293],
		[111,'八幡',34.866384,135.706555],
		[112,'飯塚',33.628487,130.689545],
		[113,'久留米',33.319778,130.501408],
		[114,'大分',33.232794,131.606595],
		[115,'中津',33.599259,131.19077],
		[116,'日田',33.317014,130.938786],
		[117,'佐伯',32.972521,131.901857],
		[118,'長崎',32.753085,129.870515],
		[119,'佐世保',33.163828,129.726415],
		[120,'厳原',34.363132,129.325117],
		[121,'福江',35.146339,136.912927],
		[122,'佐賀',33.264212,130.297608],
		[123,'伊万里',33.271862,129.876344],
		[124,'熊本',32.789385,130.688499],
		[125,'阿蘇乙姫',32.939384,131.041447],
		[126,'牛深',32.216561,130.033190],
		[127,'人吉',32.216142,130.753797],
		[128,'宮崎',31.916910,131.433921],
		[129,'延岡',32.590123,131.672441],
		[130,'都城',31.736570,131.073992],
		[131,'高千穂',32.714701,131.306448],
		[132,'鹿児島',31.602098,130.564112],
		[133,'鹿屋',31.385295,130.849937],
		[134,'西之表',30.722944,131.008189],
		[135,'名瀬',28.381746,129.498729],
		[136,'那覇',26.213157,127.689792],
		[137,'名護',26.596928,127.991097],
		[138,'久米島',26.345933,126.772260],
		[139,'南大東島',25.834629,131.232432],
		[140,'宮古島',24.801684,125.286612],
		[141,'石垣島',24.347097,124.166107],
		[142,'与那国島',24.464004,122.988577],
		);

my $parser = XML::LibXML->new;
my $ua = LWP::UserAgent->new;
$ua->agent('GeoLWRSS/0.01');
$ua->timeout(10);

while (my $q = new CGI::Fast) {
    eval {
	my $url = $q->param('url');
	die "403 Forbidden" unless $url && $url =~ m!^http://weather.livedoor.com/forecast/!;

	my $res = $ua->get($url);
	die "403 Forbidden" unless $res->is_success;
	my $xml = $parser->parse_string($res->content);

	my $rss = $xml->findnodes('/rss')->get_node(0);
	$rss->setNamespace('http://www.w3.org/2003/01/geo/wgs84_pos#', 'geo', 0);
	foreach my $item ($rss->findnodes('//item[ldWeather:lwws]')) {
	    my $lwws_id = $item->findvalue('ldWeather:lwws/@id');
	    next unless $Locations[$lwws_id - 1];
	    my $lat = $xml->createElement('geo:lat');
	    my $long = $xml->createElement('geo:long');
	    $lat->appendText($Locations[$lwws_id - 1][2]);
	    $long->appendText($Locations[$lwws_id - 1][3]);
	    $item->appendChild($lat);
	    $item->appendChild($long);
	}
	print $q->header($res->header('Content-Type')), encode('utf8', $xml->toString);
    };
    if ($@) {
	print $q->header('text/plain', '403 Forbidden'), $@;
    }
}
